import indexCreater from "./index";
import Selector from "./reselector";
import httpClient from "./common/axios";
import {FactoryInstance, Factory} from "../index.d";

const BoeAppFactory = function(this: Factory, options = {}) {
  this.options = {
    entry: "./",
    plugin: "./",
    initializer: "/getStore",
    containerID: "root",
    globalAction: {
      session: {
        actions: {
          setSession: {
            reducer: {
              next(state: any, action: any) {
                if (action.payload) {
                  return action.payload;
                }
                return state;
              }
            }
          }
        },
        initialState: {},
        mapStateToProps: (state, ownProps = {}) => {
          return {
            SESSION: state.session
          };
        }
      },
      dictionary: {
        actions: {
          setDictionary: {
            reducer: {
              next(state: any, action: any) {
                if (action.payload) {
                  return action.payload;
                }
                return state;
              }
            }
          },
          refreshDictionary: {
            reducer: {
              next(state: any, action: any) {
                if (action.payload.success) {
                  return action.payload.data;
                }
                return state;
              }
            },
            payloadCreator: () => httpClient.post("/getStore")
          }
        },
        initialState: [],
        mapStateToProps: (state, ownProps = {}) => {
          return {
            DICTIONARY: state.dictionary
          };
        }
      },
      menus: {
        actions: {
          setMenu: {
            reducer: {
              next(state: any, action: any) {
                if (action.payload) {
                  return action.payload;
                }
                return state;
              }
            }
          }
        },
        initialState: [],
        mapStateToProps: (state, ownProps = {}) => {
          return {
            MENUS: state.menus
          };
        }
      },
      menuSelection: {
        actions: {
          selectMenu: {
            reducer: {
              next(state: any, action: any) {
                if (action.payload.pattern) {
                  return action.payload.pattern;
                }
                return state;
              }
            },
            payloadCreator: a => a
          }
        },
        initialState: "",
        mapStateToProps: (state, ownProps = {}) => {
          return {
            menuSelection: state.menuSelection
          };
        }
      },
      routerParam: {
        actions: {
          setRouterParam: {
            reducer: {
              next(state: any, action: any) {
                const { pattern, param } = action.payload;
                if (pattern) {
                  return { ...state, [pattern]: param };
                }
                return state;
              }
            },
            payloadCreator: a => a
          }
        },
        initialState: {},
        mapStateToProps: (state, ownProps = {}) => {
          return {
            getComponentParam: Selector.getComponentParam(state)
          };
        }
      }
    },
    ...options
  };
};

BoeAppFactory.prototype.run = async function() {
  return await indexCreater(this.options);
};

export default {
  create: (options) => new BoeAppFactory(options) as FactoryInstance,
  httpClient
};
